/**
 * @param {number} rowsCount
 * @param {number} colsCount
 * @return {Array<Array<number>>}
 */
Array.prototype.snail = function (rowsCount, colsCount) {
    const length = this.length
    if (rowsCount * colsCount !== length) return [];

    const newArray = new Array(rowsCount).fill(0).map(() => new Array(colsCount).fill(0));
    let row = 0;
    let col = 0;
    let i = 0;
    while (i < length) {
        if (row === rowsCount - 1 || (row === 0 && col !== 0)) {
            newArray[row][col] = this[i];
            col++;
            i++;
        }
        if (row < rowsCount && col < colsCount) {
            newArray[row][col] = this[i];
        }
        if (col % 2 == 0 && row < rowsCount - 1) {
            row++;
            i++;
        }
        if (col % 2 == 1 && row !== 0) {
            row--;
            i++;
        }
    }
    return newArray;
}
const a = [19, 10, 3, 7, 9, 8, 5, 2, 1, 17, 16, 14, 12, 18, 6, 13, 11, 20, 4, 15]

console.log(a.snail(5, 4));